package com.ds.dsm.view.config.tree;

import com.ds.common.JDSException;
import com.ds.config.ListResultModel;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.CustomAnnotation;
import com.ds.esd.custom.annotation.GridViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;

import com.ds.esd.custom.bean.MethodConfig;

import com.ds.esd.custom.bean.nav.foldingtree.NavFoldingTreeViewBean;
import com.ds.esd.custom.bean.nav.tree.NavTreeViewBean;
import com.ds.esd.custom.buttonviews.annotation.ButtonViewsAnnotation;
import com.ds.esd.custom.form.enums.CustomFormMenu;
import com.ds.esd.custom.form.pop.PopTreeViewBean;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.custom.tree.CustomTreeViewBean;
import com.ds.esd.custom.tree.enums.CustomTreeEvent;
import com.ds.esd.custom.tree.enums.TreeMenu;
import com.ds.esd.custom.tree.enums.TreeRowMenu;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import com.ds.esd.tool.ui.component.tab.SideBarStatusType;
import com.ds.esd.tool.ui.enums.BarLocationType;
import com.ds.web.util.PageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RequestMapping("/dsm/view/config/tree/")
@Controller
@ButtonViewsAnnotation(barLocation = BarLocationType.top, sideBarStatus = SideBarStatusType.expand, barSize = "3em")
@MethodChinaName(cname = "树形配置")
public class TreeActionGroup {

    @CustomAnnotation(hidden = true, pid = true)
    String viewInstId;

    @CustomAnnotation(hidden = true, pid = true)
    String className;

    @CustomAnnotation(hidden = true, uid = true)
    String methodName;

    public TreeActionGroup() {

    }


    @MethodChinaName(cname = "按钮组")
    @RequestMapping(method = RequestMethod.POST, value = "CustomBottombar")
    @GridViewAnnotation(
            addPath = "TreeBottomTree")
    @ModuleAnnotation(caption = "按钮组",
            imageClass = "spafont spa-icon-c-statusbutton", index = 0)
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<TreeMenuView>> getCustomBottombar(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<TreeMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig customMethodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            Set<CustomFormMenu> customFormMenus = new HashSet<>();
            CustomTreeViewBean customTreeViewBean = null;
            if (customMethodAPIBean.getView() instanceof NavTreeViewBean) {
                NavTreeViewBean navTreeViewBean = (NavTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = navTreeViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof NavFoldingTreeViewBean) {
                NavFoldingTreeViewBean navTreeViewBean = (NavFoldingTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = navTreeViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof PopTreeViewBean) {
                PopTreeViewBean navTreeViewBean = (PopTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = navTreeViewBean.getTreeViewBean();
            } else {
                customTreeViewBean = (CustomTreeViewBean) customMethodAPIBean.getView();
            }
            customFormMenus = ((CustomTreeViewBean) customTreeViewBean).getBottombarMenu();


            resultModel = PageUtil.getDefaultPageList(customFormMenus, TreeMenuView.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }

        return resultModel;
    }


    @RequestMapping(method = RequestMethod.POST, value = "CustomMenus")
    @GridViewAnnotation(
            addPath = "TreeMenuTree")
    @ModuleAnnotation(caption = "工具栏",
            imageClass = "spafont spa-icon-c-toolbar", index = 1)
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<TreeMenuView>> getCustomMenus(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<TreeMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,  viewInstId);
            MethodConfig customMethodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            Set<TreeMenu> customFormMenus = new HashSet<>();
            CustomTreeViewBean customTreeViewBean = null;

            if (customMethodAPIBean.getView() instanceof NavTreeViewBean) {
                NavTreeViewBean navTreeViewBean = (NavTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = navTreeViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof NavFoldingTreeViewBean) {
                NavFoldingTreeViewBean navTreeViewBean = (NavFoldingTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = navTreeViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof PopTreeViewBean) {
                PopTreeViewBean treeViewBean = (PopTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = treeViewBean.getTreeViewBean();

            } else {
                customTreeViewBean = (CustomTreeViewBean) customMethodAPIBean.getView();
            }
            customFormMenus = ((CustomTreeViewBean) customTreeViewBean).getCustomMenu();
            resultModel = PageUtil.getDefaultPageList(customFormMenus, TreeMenuView.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return resultModel;
    }


    @RequestMapping(method = RequestMethod.POST, value = "RowMenus")
    @GridViewAnnotation(
            addPath = "RowMenuTree")
    @ModuleAnnotation(caption = "行按钮组",
            imageClass = "spafont spa-icon-c-statusbutton", index = 2)
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<TreeMenuView>> getRowMenus(String sourceClassName, String methodName, String domainId, String viewInstId) {
        ListResultModel<List<TreeMenuView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,  viewInstId);
            MethodConfig customMethodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            Set<TreeRowMenu> rowMenu = new HashSet<>();
            CustomTreeViewBean customTreeViewBean = null;

            if (customMethodAPIBean.getView() instanceof NavTreeViewBean) {
                NavTreeViewBean navTreeViewBean = (NavTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = navTreeViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof NavFoldingTreeViewBean) {
                NavFoldingTreeViewBean navTreeViewBean = (NavFoldingTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = navTreeViewBean.getTreeViewBean();
            } else if (customMethodAPIBean.getView() instanceof PopTreeViewBean) {
                PopTreeViewBean treeViewBean = (PopTreeViewBean) customMethodAPIBean.getView();
                customTreeViewBean = treeViewBean.getTreeViewBean();
            } else {
                customTreeViewBean = (CustomTreeViewBean) customMethodAPIBean.getView();
            }
            rowMenu = ((CustomTreeViewBean) customTreeViewBean).getRowCmdBean().getRowMenu();

            resultModel = PageUtil.getDefaultPageList(rowMenu, TreeMenuView.class);
        } catch (JDSException e) {
            e.printStackTrace();
        }

        return resultModel;
    }

    @MethodChinaName(cname = "监听事件")
    @RequestMapping(method = RequestMethod.POST, value = "BindMenus")
    @GridViewAnnotation
    @ModuleAnnotation(imageClass = "spafont spa-icon-c-statusbutton", caption = "监听事件", index = 1)
    @APIEventAnnotation(autoRun = true)
    @ResponseBody
    public ListResultModel<List<TreeEventView>> getBindMenus(String sourceClassName, String viewInstId, String domainId, String methodName) {
        ListResultModel<List<TreeEventView>> resultModel = new ListResultModel<>();
        try {
            ViewEntityConfig esdClassConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig apiCallBean = esdClassConfig.getSourceConfig().getMethodByName(methodName);
            CustomTreeViewBean customTreeViewBean = (CustomTreeViewBean) apiCallBean.getView();

            Set<CustomTreeEvent> events = customTreeViewBean.getEvent();
            List<TreeEventView> views = new ArrayList<>();
            for (CustomTreeEvent gridEvent : events) {
                views.add(new TreeEventView(gridEvent, apiCallBean));
            }
            resultModel = PageUtil.getDefaultPageList(views);
        } catch (JDSException e) {
            e.printStackTrace();
        }
        return resultModel;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public String getViewInstId() {
        return viewInstId;
    }

    public void setViewInstId(String viewInstId) {
        this.viewInstId = viewInstId;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }
}
